import docx, openpyxl
import os, sys
import random


def get_info(filename):
    file=docx.Document(filename)


    # ---------- paragraphs ----------

    paragraphs = [i.text for i in file.paragraphs]
    #print(paragraphs)

    bjh = [-1,""]
    sxqgs = [-1,""]
    gjjc = [-1,""]
    blzd = [-1,""]
    ft = [-1,""]
    czys = [-1,""]
    fzys = [-1,""]
    zdrq = [-1,""]
    pitem=(bjh,sxqgs,gjjc,blzd,ft,czys,fzys,zdrq)

    # assign index
    for index,value in enumerate(paragraphs):
        if "病检号" in value:
            bjh[0] = index
        if ("个肾小球." in value) and len(value) < 10:
            sxqgs[0] = index
        if "光镜检查" in value:
            gjjc[0] = index
        if "病理诊断" in value:
            blzd[0] = index
        if "附图" in value:
            ft[0] = index
        if "初诊医师" and "复诊医师" in value:
            czys[0] = index
            fzys[0] = index
        if "诊断日期" in value:
            zdrq[0] = index

    if -1 in [j[0] for j in pitem]:
        print("检查格式")

    # assign value
    try:
        bjh[1] = paragraphs[bjh[0]].split(":")[1].strip()
    except IndexError:
        print("病检号格式可能有误")

    try:
        sxqgs[1] =  paragraphs[sxqgs[0]].split("个")[0].strip()
    except IndexError:
        print("肾小球个数格式可能有误")

    t1=""
    for i in range(gjjc[0]+1,blzd[0]):
        t1+=paragraphs[i]
    gjjc[1]=t1.strip()

    t2=""
    for i in range(blzd[0]+1,ft[0]):
        t2+=paragraphs[i]
    blzd[1]=t2.strip()

    try:
        czys[1] = paragraphs[czys[0]].split("：")[1].split("复")[0].strip()
    except IndexError:
        print("初检医师格式可能有误")
        czys[1] = ""
    try:
        fzys[1] = paragraphs[fzys[0]].split("：")[1].split("复")[2].strip()
    except IndexError:
        #print("复检医师格式可能有误")
        fzys[1] = ""
    try:
        zdrq[1] = paragraphs[zdrq[0]].split("：")[1].strip()
    except IndexError:
        print("诊断日期格式可能有误")

    #print(pitem)


    # ---------- table ----------

    tables = file.tables
    table0 = tables[0]
    table1 = tables[1]

    xm = table0.cell(0, 0).text.split("\n")[0].split()[1].strip()
    xb = table0.cell(0, 1).text.split("\n")[0].split("：")[1].strip()
    nl = table0.cell(0, 3).text.split("\n")[0].split("：")[1].strip().strip("岁")
    blh = table0.cell(0, 0).text.split("\n")[1].split("：")[1].strip()
    lczd = table0.cell(0, 1).text.split("\n")[1].split("：")[1].strip()
    sjdw = table0.cell(1, 0).text.split("：")[1].strip()
    sjys = table0.cell(1, 1).text.split("：")[1].strip()
    sjcl = table0.cell(1, 2).text.split("：")[1].strip()
    sjrq = table0.cell(1, 4).text.split("：")[1].strip()
    iga = (table1.cell(0, 0).text.split("：")[2], table1.cell(0, 1).text.split("：")[1], table1.cell(0, 2).text.split("：")[1])
    igg = (table1.cell(1, 0).text.split("：")[2], table1.cell(1, 1).text.split("：")[1], table1.cell(1, 2).text.split("：")[1])
    igm = (table1.cell(2, 0).text.split("：")[2], table1.cell(2, 1).text.split("：")[1], table1.cell(2, 2).text.split("：")[1])
    c1q = (table1.cell(3, 0).text.split("：")[2], table1.cell(3, 1).text.split("：")[1], table1.cell(3, 2).text.split("：")[1])
    c3 =  (table1.cell(4, 0).text.split("：")[2], table1.cell(4, 1).text.split("：")[1], table1.cell(4, 2).text.split("：")[1])
    fra = (table1.cell(5, 0).text.split("：")[2], table1.cell(5, 1).text.split("：")[1], table1.cell(5, 2).text.split("：")[1])
    
    titem = (xm, xb, nl, blh, lczd, sjdw, sjys, sjcl, sjrq, iga, igg, igm, c1q, c3, fra)
    #print(titem)

    return([bjh[1],
            xm, xb, nl, blh, lczd, sjdw, sjys, sjcl, sjrq,
            iga[0], iga[1], iga[2], 
            igg[0], igg[1], igg[2], 
            igm[0], igm[1], igm[2], 
            c1q[0], c1q[1], c1q[2], 
             c3[0],  c3[1],  c3[2], 
            fra[0], fra[1], fra[2], 
            sxqgs[1],
            gjjc[1],
            blzd[1],
            czys[1],fzys[1],zdrq[1]])


def write_xlsx(output):
    wb = openpyxl.Workbook()
    sheet = wb["Sheet"]
    for i in output:
        sheet.append(i)
    
    rand = str(int(random.randrange(10000,99999)))
    wb.save("summary_"+rand+".xlsx")




header = ["病检号",
            "姓名", "性别", "年龄", "病历号", "临床诊断", "送检单位", "送检医生", "送检材料", "送检日期",
            "IgA 强度", "IgA 沉积方式", "IgA 部位", 
            "IgG 强度", "IgG 沉积方式", "IgG 部位", 
            "IgM 强度", "IgM 沉积方式", "IgM 部位", 
            "C1q 强度", "C1q 沉积方式", "C1q 部位", 
            "C3 强度" , "C3 沉积方式" , "C3 部位" , 
            "FRA 强度", "FRA 沉积方式", "FRA 部位", 
            "肾小球个数",
            "光镜检查",
            "病理诊断",
            "初诊医师", "复诊医师", "诊断日期"]

output_content = []
output_content.append(header)


for f in os.listdir("."):
    if ".docx" in f:
        output_content.append(get_info(f))

write_xlsx(output_content)




#get_info(sys.argv[1])